﻿Namespace Save

    ''' <summary>
    ''' テーブルアーカイブ生成クラス
    ''' </summary>
    ''' <remarks></remarks>
    Public Class TableArchiveFactory

        ''' <summary>
        ''' テーブルアーカイブを作成します
        ''' </summary>
        ''' <param name="instance"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function Create(instance As Object) As TableArchive
            Return Create(instance, instance.GetType)
        End Function

        ''' <summary>
        ''' テーブルアーカイブを作成します
        ''' </summary>
        ''' <param name="instance"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function Create(instance As Object, t As Type) As TableArchive
            Dim arch As New TableArchive
            arch.ModelKey = ModelKeyFactory.Create(instance)
            arch.TypeFullName = t.FullName
            For Each item In ColumnArchiveFactory.Create(instance, t)
                item.TableArchive = arch
                arch.ColumnArchives.Add(item.Key, item)
            Next

            'スーパークラスのアーカイブ
            If t.ToTable.IsRootBase = False Then arch.SuperTableArchive = TableArchiveFactory.Create(instance, t.BaseType)

            '子クラスのアーカイブ
            For Each item In ListArchiveFactory.Create(instance, t)
                arch.ChildListArchives.Add(item.Key, item)
            Next

            Return arch
        End Function

    End Class

End Namespace
